var initPageModule = (function () {

  var oPageBtnCover = document.getElementById('pageBtnCover'),
    pageNum = 20,
    curPage = 1;
  //初始化
  setPageBtns(pageNum, curPage);

  oPageBtnCover.onclick = function (e) {
    var e = e || window.event,
      tar = e.target || e.srcElement,
      className = tar.className;

    switch (className) {
      case 'page-btn':
        curPage = parseInt(tar.getAttribute('data-page'));
        setPageBtns(pageNum, curPage);
        break;
      case 'prev-btn':
        curPage -= 1;
        setPageBtns(pageNum, curPage);
        break;
      case 'next-btn':
        curPage += 1;
        setPageBtns(pageNum, curPage);
        break;
    }
  }

  /**
   * 设置页码按钮
   * @param {*} pageNum 总页码
   * @param {*} curPage 当前页
   */
  function setPageBtns(pageNum, curPage) {
    /**
     * 模拟一个分页的模板
     * @param {*} type 类型
     * @param {*} num 页码
     * @param {*} curPage 当前页
     */
    var pageBtnTpl = function (type, num, curPage) {
      switch (type) {
        //按钮状态
        case 'btn':
          //是否可以点击
          if (curPage == num) {
            //当前页码 且不能点击
            return '<span class="cur-page">' + num + '</span>';
            //'<span class="cur-page">1</span>'
          } else {
            //可以点击的页码
            //data-page记录第几页数据
            return '<a href="javascript:; class="page-btn" data-page="' + num + '">' + num + '</a>';
          }
          break;
        case 'prev':
          if (curPage == 1) {
            return '<span class="disabled">&lt;</span>';
          } else {
            return '<a href="javascript:; class="prev-btn">&lt;</a>';
          }
          break;
        case 'next':
          if (curPage == pageNum) {
            return '<span class="disabled">&gt;</span>';
          } else {
            return '<a href="javascript:; class="next-btn">&gt;</a>';
          }
          break;
        case 'points':
          return '<span>...</span>';
          break;
      }
    }

    var btnGroup = '';

    function makeBtnGroup(start, end, curPage) {
      for (var i = start; i <= end; i++) {
        btnGroup += pageBtnTpl('btn', i, curPage);
      }
    }

    btnGroup += pageBtnTpl('prev', pageNum, curPage);

    if (pageNum > 7) {
      //第一种情况 页码1 页码2 页码3 ... 页码n 页码n+1
      //小于3个时，循环3个按钮，其中两个可以点击，另外一个不能点击
      if (curPage < 3) {
        makeBtnGroup(1, 3, curPage);
        btnGroup += pageBtnTpl('points');
        makeBtnGroup(pageNum - 1, pageNum, curPage);
      } else if (curPage >= 3 && curPage < 5) {
        //第二种情况 
        makeBtnGroup(1, curPage + 1, curPage);
        btnGroup += pageBtnTpl('points');
        makeBtnGroup(pageNum - 1, pageNum, curPage);
      } else if (curPage >= 5 && curPage < pageNum - 3) {
        //第三种情况 
        makeBtnGroup(1, 2, curPage);
        btnGroup += pageBtnTpl('points');
        makeBtnGroup(curPage - 1, curPage + 1, curPage);
        btnGroup += pageBtnTpl('points');
        makeBtnGroup(pageNum - 1, pageNum, curPage);
      } else if (curPage >= pageNum - 4 && curPage <= pageNum - 1) {
        //第四种情况
        makeBtnGroup(1, 2, curPage);
        btnGroup += pageBtnTpl('points');
        makeBtnGroup(curPage - 1, pageNum, curPage);
      } else if (curPage == pageNum) {
        //第五种情况
        makeBtnGroup(1, 2, curPage);
        btnGroup += pageBtnTpl('points');
        makeBtnGroup(curPage - 2, pageNum, curPage);
      }
    } else {
      //第六种情况
      makeBtnGroup(1, pageNum, curPage);
    }
    btnGroup += pageBtnTpl('next', pageNum, curPage);
    oPageBtnCover.innerHTML = btnGroup;
  }
})();